package org.syncsoft.overalt.gui.impl.window.commit;

import java.util.Collection;
import java.util.Set;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.document.entity.Permission;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractSendFileWindowController;

/**
 * 
 * @author tptfc
 *
 */
public class CommitWindowController extends AbstractSendFileWindowController<CommitWindow> {	
	public CommitWindowController(String filePath) {
		super(filePath);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	public void doSubmit(WindowContextValue context) throws SystemException {
		String title = context.getValue("title");
		String description = context.getValue("description");
		String versionDescription = context.getValue("versionDescription");
		
		String permissionString = context.getValue("permission");
		Permission permission = parsePermission(permissionString);
		
		Collection<Integer> communityCodes = context.getValues("associetedCommunities");
		Set<Community> communities = parseCommunities(communityCodes);
		
		String tagsString = context.getValue("tags");
		Set<Tag> tags = parseTags(tagsString);
		
		getCore().commit(getFilePath(), title, description, versionDescription, communities, permission, tags);
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#createWindow()
	 */
	@Override
	public CommitWindow doCreateWindow() throws SystemException {
		CommitWindow window = new CommitWindow(this);
		
		Collection<Community> communities = getCore().getComunities();
		window.setCommunities(communities);
		
		return window;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.api.window.AbstractWindowController#getUpdateWindowContextValue()
	 */
	@Override
	protected WindowContextValue getUpdateWindowContextValue() throws SystemException {
		WindowContextValue context = super.getUpdateWindowContextValue();
		
		String title = getCore().getFileTitle(getFilePath());
		context.setValue("title", title);
		
		String description = getCore().getFileTitle(getFilePath());
		context.setValue("description", description);
		
		return context;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.api.window.AbstractSendFileWindowController#getUpdateRequiredTags(java.lang.String)
	 */
	@Override
	protected Collection<Tag> getUpdateRequiredTags(String filePath) throws SystemException {
		return getCore().getFileTags(getFilePath());
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.api.window.AbstractSendFileWindowController#getUpdateRequiredCommunities(java.lang.String)
	 */
	@Override
	protected Collection<Community> getUpdateRequiredCommunities(String filePath) throws SystemException {
		return getCore().getFileCommunities(filePath);
	}
	
	public static void main(String[] args) {
		CommitWindowController c = new CommitWindowController("/home/tptfc/Developer/java/syncsoft/amigos-dropbox-fork/a.txt");
		c.start();
	}
}
